package com.tyning.leetcode.jz30;

import java.util.ArrayList;
import java.util.Stack;

public class MinStack {
    int[] data;
    int[] dataSort;
    int head;
    int end;
    int size;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        data = new int[20000];
        dataSort = new int[20000];
        head = -1;
        end = -1;
        size = 0;
    }

    public void push(int x) {
        if (size == 0) {
            ++head;
            ++end;
            dataSort[end] = x;
        } else {
            int min = dataSort[end];
            ++end;
            if (min > x) {
                dataSort[end] = x;
            } else {
                dataSort[end] = min;
            }
        }
        data[end] = x;
        ++size;


    }

    public void pop() {
        --end;
        --size;
        if (size == 0) {
            --head;
        }

    }

    public int top() {
        return data[end];
    }

    public int getMin() {
        return dataSort[end];
    }
}
